/*
============================================================================
 Name        : dd_control.h
 Author      : Jian(raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : dd_control.h - header file
============================================================================
*/

#ifndef DD_CONTROL_H
#define DD_CONTROL_H

#include "dd_define.h"
#include "dd_ui_base.h"
#include "pd_gui.h"

typedef struct __dd_control dd_control ;


struct __dd_control {
    void        (*destory)(void* ctl) ;
    void        (*draw)(dd_control* ctl, pd_gc* gc) ;
    void        (*update)(dd_control* ctl) ;
    void        (*set_focus)(dd_control* ctl, DD_BOOL is_focus, DD_BOOL from_forward) ;
    void        (*set_visible)(dd_control* ctl, DD_BOOL is_visible) ;
    DD_INT      (*child_count)(dd_control* ctl) ;
    dd_control* (*child)(dd_control* ctl, DD_INT index) ; 
    DD_BOOL     (*is_accept_focus)(dd_control* ctl) ;
    void        (*set_rect)(dd_control* ctl, dd_rect rect) ;
    DD_BOOL     (*key_handle)(dd_control* ctl, dd_key_code code, dd_key_type type) ;
    
    DD_BOOL     is_visible ;
    DD_BOOL     is_focus ;
    dd_rect     rect ;
} ;

void control_defult_fun_init(dd_control* ctl) ;

void control_def_destroy(void* ctl) ;

void control_def_draw(dd_control* ctl, pd_gc* gc) ;

void control_def_update(dd_control* ctl) ;

void control_def_set_focus(dd_control* ctl, DD_BOOL is_focus, DD_BOOL from_forward) ;

void control_def_set_visible(dd_control* ctl, DD_BOOL is_visible) ;

DD_INT control_def_child_count(dd_control* ctl) ;

dd_control* control_def_child(dd_control* ctl, DD_INT index) ; 

DD_BOOL control_def_is_accept_focus(dd_control* ctl) ;

void control_def_set_rect(dd_control* ctl, dd_rect rect) ;

DD_BOOL control_def_key_handle(dd_control* ctl, dd_key_code code, dd_key_type type) ;


#ifdef __DD_DEBUG
DD_BOOL control_is_focus(void* ctl) ;
DD_BOOL control_is_visible(void* ctl) ;
#else
#define control_is_focus(c)   (((dd_control*)(c))->is_focus) 
#define control_is_visible(c) (((dd_control*)(c))->is_visible) 
#endif

#define control_destroy(c)           do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->destory, break;); ((dd_control*)(c))->destory(c);}while (0) 
#define control_draw(c)              do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->draw, break;); ((dd_control*)(c))->draw(c);}while (0) 
#define control_update(c)            do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->update, break;); ((dd_control*)(c))->update(c);}while (0) 
#define control_set_focus(c, f)      do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->set_visible, break;); ((dd_control*)(c))->set_visible(c,f);}while (0) 
#define control_set_visible(c, v)    do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->draw, break;); ((dd_control*)(c))->draw(c);}while (0) 
#define control_child_count(c)       do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->child_count, break;); ((dd_control*)(c))->child_count(c);}while (0) 
#define control_child(c, i)          do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->child, break;); ((dd_control*)(c))->child(c, i);}while (0) 
#define control_is_accept_focus(c)   do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->is_accept_focus, break;); ((dd_control*)(c))->is_accept_focus(c);}while (0) 
#define control_set_rect(c, r)       do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->set_rect, break;); ((dd_control*)(c))->set_rect(c, r);}while (0) 
#define control_key_handle(c, d, t)  do {__DD_ASSERT_DEFENSE(c && ((dd_control*)(c))->key_handle, break;); ((dd_control*)(c))->key_handle(c, d, t);}while (0) 


#endif // DD_CONTROL_H

/* End of File */
